<%@ page import="net.pws.oos.security.SecurityUtils"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="../../../header.jsp"%>

<div class="border bg">
<div class="page-header">
	<h2>用户管理<small>(<span id="currentCount">0</span>/<span id="totalCount">0</span>)</small></h2>
	<div class="cms_toolbar">
		<% if(SecurityUtils.allowedResource("/sm/um/create")){ %>
		<a id="_create_item_action" href="edit.do" class="btn"><i class="icon-plus"></i>新建</a>
		<%} %>
		<a id="exportButton" href="#" onclick="exportExcel()" class="btn"><i class="icon-share"></i>导出</a>
	</div>
</div>

<div class="text-right form-inline">
	<form id="searchForm" action="">
		<select name="roleId" id="roleId" class="input-small">
			<option value="">所有角色</option>
			<c:forEach var="role" items="${roles}">
				<option value="${role.id}">${role.name}</option>
			</c:forEach>
		</select>
		<input type="hidden" name="organId" id="organId">
		<input type="text" name="organName" id="organName" class="input-small" placeholder="点击选择部门..." onclick="selectOrgan()"/>
		<input type="text" name="name" id="name" class="input-medium" placeholder="请输入姓名/账号/手机..."/>
		<a class="btn" href="javascript:;" onclick="refresh()">查询</a>
	</form>
</div>

<div>
	<br />
	<table id="listTable" class="table table-striped cms-table">
		<thead>
			<tr>
				<th width="60">姓名</th>
				<th width="90">账号</th>
				<th width="80">手机号码</th>
				<th>组织机构</th>
				<th width="30">Web</th>
				<th width="30">手机</th>
				<th width="90">角色</th>
				<th width="40">操作</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
	<div class="well cms_more">
		<a href="javascript:;" onclick="moreData()"><i class="icon-arrow-down"></i>查看更多...</a>
	</div>
</div>

</div>

</div>

<table id="listTemplate" class="hide">
  	<tr>
	    <td><a href=""></a></td>
	    <td></td>
	    <td></td>
	    <td></td>
	    <td></td>
	    <td></td>
	    <td></td>
	    <td><a href="#">删除</a></td>
  	</tr>
</table>

<script type="text/javascript">
var pageIndex = 0, pageSize = 20, currentCount = 0, totalCount = 0;

function selectOrgan() {
	$.openDialog("#dialog",
			{height:350,
			width:300,
			minHeight:350,
			minWidth:300,
			loadPage:ctx+"/biz/system/organ/query.jsp",
			title:"选择组织机构"},
			function(data) {
				$("#organId").val(data.data("id"));
				$("#organName").val(data.data("name"));
			});
}

function count(){
	block(".cms_more", "正在加载数据...");
	$.ajax({
    	type: "GET",
    	cache: false,
    	url: "ourCount.json",
    	data: $("#searchForm").serialize(),
    	dataType:"json",
    	success:function(json) {
    		unblock(".cms_more");
    		if (!json.succeed) {
    			$.alert(json.message);
    			return;
    		}
    		totalCount = json.data;
			$("#totalCount").text(json.data);
			list();
    	},
		error: function(message) {
			unblock(".cms_more");
			$.alert(message);
		}
	});
}

function list(){
	block(".cms_more", "正在加载数据...");
	$.ajax({
    	type: "GET",
    	cache: false,
    	url: "ourList.json",
    	data: $("#searchForm").serialize() + "&start=" + pageIndex + "&limit=" + pageSize,
    	dataType:"json",
    	success:function(json) {
    		unblock(".cms_more");
    		if (!json.succeed) {
    			$.alert(json.message);
    			return;
    		}
			buildList(json.data);
    	},
		error: function(message) {
			unblock(".cms_more");
			$.alert(message);
		}
	});
}

function refresh(){
	pageIndex = 0;
	currentCount = 0;
	totalCount = 0;
	
	$("#currentCount").text(currentCount);
	$("#totalCount").text(totalCount);
	$("#listTable").find("tbody").empty();
	$(".cms_more").show();
	count();
	//list();
}

function moreData(){
	pageIndex++;
	list();
}

function buildList(data){
	if(data.length == 0){
		$(".cms_more").hide();
		return;
	}
	currentCount += data.length;
	$("#currentCount").text(currentCount);
	if(currentCount >= totalCount){
		$(".cms_more").hide();
	} else {
		$(".cms_more").show();
	}
	$(data).each(function(index, item) {
		buildTd(item).appendTo($("#listTable").find("tbody"));
	});
}

function buildTd(item){
	var taskEle = $("#listTemplate").clone();
	taskEle.find("tr").attr("id", "item" + item.id);
	var tdList = taskEle.find("td");
	$(tdList.get(0)).find("a").attr("href", "edit.do?id=" + item.id);
	$(tdList.get(0)).find("a").text(item.name);
	$(tdList.get(1)).text(item.account);
	$(tdList.get(2)).text(item.mobile ? item.mobile : "");
	$(tdList.get(3)).text(item.organName);
	$(tdList.get(4)).html(item.webAccessAllowed?"允许":"<span style='color:#ff0000'>拒绝</span>");
	$(tdList.get(5)).html(item.mobileAccessAllowed?"允许":"<span style='color:#ff0000'>拒绝</span>");
	$(tdList.get(6)).text(item.roleName ? item.roleName : "");
	$(tdList.get(7)).find("a").attr("href", "javascript:deleteData('" + item.id + "')");
	
	return taskEle.find("tr");
}

function deleteData(id) {
	if (!confirm("你确定删除此用户吗？")){
		return;
	}
	block("#listTable", "处理中...");
	$.ajax({
    	type: "GET",
    	cache: false,
    	url: "delete.json",
    	data: {id : id},
    	dataType:"json",
    	success:function(json) {
    		unblock("#listTable");
    		if (!json.succeed) {
    			$.alert(json.message);
    			return;
    		}
    		refresh();
    	},
		error: function(message) {
			unblock("#listTable");
			$.alert(message);
		}
	});
}

function exportExcel(){
	var url = "excel.do?" + $("#searchForm").serialize();
	window.open(url);
}

$(document).ready(function() {	
	refresh();
});
</script>

<%@ include file="../../../footer.jsp"%>